package com.ts;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AdditionActivity extends Activity {
	private TextView textView;
	List<String> showLogs = new ArrayList<String>();
	private int clickCount = 0;
	String showText;
	boolean clickAll = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		System.out.println("ScondActivity ------------- onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addition);
		textView = (TextView) findViewById(R.id.myTextViewAddition);
		String showStr = "";
		String showStr2 = "";
		Intent intent = getIntent();
		if (intent != null) {
			showStr = intent.getStringExtra("showStr");
			showStr2 = intent.getStringExtra("showStr2");
		}
		for (int i = 0; i <= 14; i++) {
			showLogs.add(getString(R.string.showDialog1 + i));
		}
		System.out.println(showLogs);
		showText = showStr + " str2 " + showStr2;
		DisplayMetrics displayMetrics = new DisplayMetrics();
		//调用getWindowManager 方法之后会取得现有的activity的handler 此时调用getDefaultDisplay方法奖取得的宽高维护存于与
		//DisplayMetrics对象dm中 取得的宽高唯独以像素(pixel)单位 此处的像素指的是绝对像素 而非相对像素
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

		String displayMetricsStr = "手机屏幕分辨率为 : " + displayMetrics.widthPixels + "  " + displayMetrics.heightPixels;
		showText = showText + "\n" + displayMetricsStr;
		textView.setText(showText);
		textView.setTextColor(Color.BLUE);
		Button button = (Button) findViewById(R.id.additionButton01);
		button.setText(R.string.clickme);
		button.setOnClickListener(new myAdditionButtonListener(textView));
		
		Button button2 = (Button) findViewById(R.id.additionButton02);
		button2.setText(R.string.button2);
		button2.setOnClickListener(new myAdditionButtonListener2(textView));
		
		Button nextButton = (Button) findViewById(R.id.nextButton);
		nextButton.setOnClickListener(new NextButtonClickListener());
	}

	//回调函数 当点击menu键的时候调用此函数
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 1, R.string.exit1);
		menu.add(0, 2, 2, R.string.about1);
		return super.onCreateOptionsMenu(menu);
	}

	//当点击菜单项的时候调用 此函数 
	//item 被点击的菜单项
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 1) {
			finish();
		}
		return super.onOptionsItemSelected(item);
	}

	protected void onStart() {
		System.out.println("ScondActivity ------------- onStart");
		super.onStart();
	}

	protected void onRestart() {
		System.out.println("ScondActivity ------------- onRestart");
		super.onRestart();
	}

	private class myAdditionButtonListener implements OnClickListener {
		private TextView showTextView;

		public myAdditionButtonListener(TextView showTextView) {
			this.showTextView = showTextView;
		}

		@Override
		public void onClick(View v) {
			int clickCount = AdditionActivity.this.clickCount;
			if (clickCount > 14) {
				clickCount = 14;
			}
			if(clickCount == 14) {
				clickAll = true;
			}
			showTextView.setText(AdditionActivity.this.showLogs.get(clickCount));
			AdditionActivity.this.clickCount++;
		}
	}
	
	private class myAdditionButtonListener2 implements OnClickListener {
		private TextView showTextView;

		public myAdditionButtonListener2(TextView showTextView) {
			this.showTextView = showTextView;
		}

		@Override
		public void onClick(View v) {
			showTextView.setText(AdditionActivity.this.showText);
		}
	}

	protected void onResume() {
		System.out.println("ScondActivity ------------- onResume");
		super.onResume();
	}

	protected void onPause() {
		System.out.println("ScondActivity ------------- onPause");
		super.onPause();
	}

	protected void onStop() {
		System.out.println("ScondActivity ------------- onStop");
		super.onStop();
	}

	protected void onDestroy() {
		System.out.println("ScondActivity ------------- onDestroy");
		super.onDestroy();
	}
	
	private class NextButtonClickListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			if(!clickAll) {
				intent.setClass(AdditionActivity.this, DialogActivity.class);
			} else {
				intent.setClass(AdditionActivity.this, ForthActivity.class);
			}
			AdditionActivity.this.startActivity(intent);
		}
	}
}
